package org.recursed.simpleweather;

import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;

public class GeocodePlace implements Runnable {

	private String place;
	private AddLocation owner; 
	private Context context;
	
	GeocodePlace(String place, AddLocation owner, Context context) {
		this.place = place;
		this.owner = owner;
		this.context = context;
	}
	public void run() {
		Geocoder geocoder = new Geocoder(context);
		List<Address> addresses = null;
		boolean found = false;
		try {
			addresses = geocoder.getFromLocationName(place, 1);
			if (addresses != null && addresses.size() > 0) {
				found = true;
				owner.geocodeStatus = AddLocation.GEOCODE_STATUS_SUCCESS;
			} else {
				owner.geocodeStatus = AddLocation.GEOCODE_STATUS_LOOKUP_FAIL;
			}
		} catch (IOException e) {
			owner.geocodeStatus = AddLocation.GEOCODE_STATUS_NETWORK_ERROR;
			found = false;
		}
		if (found) {
			Address a = addresses.get(0);
			owner.geocoded = a;
		}
		owner.handler.post(owner.grh);
	}
}
